//
//  GradientImageView.m
//  Lafaso
//
//  Created by chengjinliang on 14-7-10.
//  Copyright (c) 2014年 Lafaso. All rights reserved.
//

#import "GradientImageView.h"
#import "UrlImageView.h"
#import "SDWebImageManager.h"
#import "UIView+Ext.h"

@interface GradientImageView ()
{
    UrlImageView* _urlImageView;
    UIImageView* _placeholderImageView;
    UIView* _borderView;
}

@end

@implementation GradientImageView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _placeholderImageView = [[UIImageView alloc] initWithFrame:self.bounds];
        [self addSubview:_placeholderImageView];
        
        _urlImageView = [[UrlImageView alloc] initWithFrame:self.bounds];
        _urlImageView.backgroundColor = [UIColor clearColor];
        [self addSubview:_urlImageView];
        
        _urlImageView.gridentAnimation = ^{
            CAAnimation *theAnimation2 = [self animationWithOpacityFrom:1.0 To:0.0 Duration:1.1 BeginTime:0];
            [_placeholderImageView.layer addAnimation:theAnimation2 forKey:nil];

            CAAnimation *theAnimation = [self animationWithOpacityFrom:0.0 To:1.0 Duration:1.1 BeginTime:0];
            [_urlImageView.layer addAnimation:theAnimation forKey:nil];

        };
        
        _borderView= [[UIView alloc] initWithFrame:self.bounds];
        _borderView.backgroundColor = [UIColor clearColor];
        _borderView.layer.borderWidth = 0.5;
        _borderView.layer.borderColor = [UIColor grayColor].CGColor;
        [self addSubview:_borderView];
    }
    
    return self;
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    _placeholderImageView.hidden = YES;
}

- (void)dealloc
{
    [_placeholderImageView release];
    _placeholderImageView = nil;
    
    [_urlImageView release];
    _urlImageView = nil;
    
    [_borderView release];
    _borderView = nil;
    
    [super dealloc];
}

- (void)setPlaceholderImage:(UIImage* )image {
    _placeholderImageView.image = image;
    _placeholderImageView.hidden = NO;
    _placeholderImageView.alpha = 1.0f;
}

- (void)setImageWithURL:(NSURL *)url {
    [_urlImageView setImageWithURL:url placeholderImage:nil];
}

- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
{
    [_urlImageView setImageWithURL:url placeholderImage:nil];
    [self setPlaceholderImage:placeholder];
}

- (void)setBorderDisplay:(BOOL)isDisplay {
    _borderView.hidden = isDisplay;
}
@end
